#pragma  once
#include "stdafx.h"
#include "units.h"
#include "scene.h"
#include "automatic_item.h"
#include "hydraulic_item.h"
//#include "connector.h"
//#include "sensor.h"
//#include "hydraulic_item.h"

//class ItemScene;
class SceneView;
//class Connector;
class hydConPoint;
class Title;
//class automaticItem;
//class Spot;
class Indicator;
//class Sensor;
class autConPoint;
class hydConPoint;

class Line : public QObject, public QGraphicsLineItem
{
	Q_OBJECT
public:

	Line(const QPointF &_source, const QPointF &_dest, const int _width);
	~Line();
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	QPointF source, dest;
	enum pipe_orientation_t{VERTICAL, HORIZONTAL, ZERO, NONE};
	pipe_orientation_t orientation();
	void setMovable(bool _allow_move){ allow_move = _allow_move; }
	double getLength();
	QPointF getPointfromPercentPos(const double &_percent_pos);
	bool isHorizontal(){ return orientation() == HORIZONTAL; }
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
private:
	bool allow_move;
	bool movable;
	const int width;
signals:
	void positionChanged(Line *_line);
	void selectionChanged(bool selected);
	void popup(QGraphicsSceneContextMenuEvent *event);
};

//===============================================================================================================

class ConnectorClass: public QGraphicsObject
{
	Q_OBJECT
public:

	//ConnectorClass(SceneView* _scene, const QPointF &_source_point, const QPointF &_dest_point, const QPointF &_source_direction, const QPointF &_dest_direction, const QString &_coordinates);
	~ConnectorClass();
	void adjust();
	QRectF boundingRect() const{return QRectF();};
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
	bool isSelected();
	
	void setSourceDirection(const QPointF &point){ source_direction = point; }
	void setDestDirection(const QPointF &point){ dest_direction = point; }
	QString get_lines_coordinate(int from_index = -1, QPointF from_pos = QPointF());
	pair<QPointF, int> intersected(const QPointF &_point);
	vector<Line*> lines;
	void setMovable(bool movable);
	QPointF getMiddlePoint();
	//Spot* spot;
	void addLine(int index, const QPointF &_source, const QPointF &_dest);
	void add_lines(const QString &coordinates);
	QPointF source_point;
	QPointF dest_point;
	QPointF source_direction;
	QPointF dest_direction;
	SceneView*  scene;
	void setWidth(const int &w){ width = w; }
private:
	void resizeNext(int index, const QPointF &new_point);
	void resizePrevious(int index, const QPointF &new_point);
	int getWidth(const QPointF &_source, const QPointF &_dest) const;
	void deleteLine(int index);
	QString getDefaultName();
	int width;
	int findIndex(Line* line);
	//enum orientation_t{VERTICAL, HORIZONTAL, ERR};
	int getOrientation(const QLineF &line);
	//QPointF getNewPoint(const int &type) const;
private slots:
	void userChangePosition(Line* _line);
	void popupShow(QGraphicsSceneContextMenuEvent *event){}
public slots:
	void setSourcePoint(const QPointF &point){ source_point = point; }
	void setDestPoint(const QPointF &point){ dest_point = point; }
//signals:
	//void popup(QGraphicsSceneContextMenuEvent *event);
};


class hydConnector: public ConnectorClass
{
	Q_OBJECT
public:
	hydConnector(SceneView* _scene, hydConPoint *_source, hydConPoint *_dest, const QString &_coordinates);
	~hydConnector();
	hydConPoint* getSource(){ return source; } 
	hydConPoint* getDest(){ return dest; } 
	void setDest(hydConPoint* point){ dest = point; }
private:
	hydConPoint *source, *dest;
	QPointF getDirection(const int &type) const;
private slots:
	void removeConnector();
	void popupShow(QGraphicsSceneContextMenuEvent *event);
};


class autConnector: public ConnectorClass
{
	Q_OBJECT
public:
	autConnector(SceneView* _scene, autConPoint *_source, autConPoint *_dest, const QString &_coordinates);
	~autConnector();
	autConPoint* getSource(){ return source; } 
	autConPoint* getDest(){ return dest; } 
	void setDest(autConPoint* point){ dest = point; }
private:
	autConPoint *source, *dest;
	QPointF getDirection(const int &type) const;
private slots:
	void removeConnector();
	void popupShow(QGraphicsSceneContextMenuEvent *event);
};


//===============================================================================================================

class virtualConnector: public QGraphicsObject
{
	Q_OBJECT
public:
	virtualConnector(SceneView* _scene, hydConPoint* _hyd_source, const QPointF &_dest_point);
	virtualConnector(SceneView* _scene, autConPoint* _aut_source, const QPointF &_dest_point);
	~virtualConnector();
	void adjust(const QPointF &_dest_point);
	QRectF boundingRect() const{ return QRectF(); };
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	hydConPoint* getHydSource(){ return hyd_source; };
	autConPoint* getAutSource(){ return aut_source; };
	Line* getLine(){ return line; };
	bool isHydraulic(){ return hyd_source != NULL; }
	bool isAutomatic(){ return aut_source != NULL; }
private:
	hydConPoint* hyd_source;
	autConPoint* aut_source;
	Line* line;
	SceneView*  scene;
};


//=====================================================================================================================
/*
class Spot : public QGraphicsObject
{
	Q_OBJECT

public:
	Spot(SceneView* _scene);
	~Spot();
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	void adjust();
	void addSource(Connector* _source);
	QPointF findClosestPoint(const QPointF &_point);
	
	void setTitle(Title* _title);
	QString getTitleText();
	QPointF getTitlePos(); 
	void setTitlePos(const QPointF &_pos); 
	void setTitleText(const QString &_text);
	automaticItem* getAutomaticItem(){ return aut_item; }
	void setAutomaticItem(automaticItem* _aut_item);
private:
	automaticItem *aut_item;
	SceneView* scene;
	Connector* source;
	Title* title;
	int line_index;
	double percent_pos;
	Line::pipe_orientation_t orientation;
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private slots:
	void showIndicator();
	void showSensorEditor();
	void showIndicatorLine();
	void removeItem();
};*/

